﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Drawing;

namespace Shapefile
{
    public class ESRIPoint : ESRIShape
    {
        private PointD point_;

        protected internal ESRIPoint(int recordNumber, StringDictionary metaData, IDataRecord dataRecord, byte[] shapeData)
            : base(ShapeType.Point, recordNumber, metaData, dataRecord)
        {
            if (shapeData == null)
            {
                throw new ArgumentNullException("ShapeData is Null!");
            }

            /*
                Position     Field       Value   Type        Number  Order
                Byte 0       Shape Type  1       Integer     1       Little
                Byte 4       X           X       Double      1       Little
                Byte 12      Y           Y       Double      1       Little
            */

            if (shapeData.Length != 28)
            {
                throw new InvalidOperationException("Invalid shape data");
            }

            point_ = new PointD(BinaryConverter.ToDouble(shapeData, 12, ByteOrder.Little),
                BinaryConverter.ToDouble(shapeData, 20, ByteOrder.Little));

        }

        public PointD Point
        {
            get { return point_; }
        }

        public override void Render(Graphics g)
        {
            float sx = g.Transform.Elements[0];
            float sy = g.Transform.Elements[3];
            Brush brush = new SolidBrush(Color.Red);
            g.FillEllipse(brush, Convert.ToSingle(point_.X), Convert.ToSingle(point_.Y), 5/sx, -5/sy);
        }
    }
}
